package cn.com.hfuu.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.com.hfuu.ViewExcel;
import cn.com.hfuu.module.code;
 
import cn.com.hfuu.module.warehousetea;
import cn.com.hfuu.module.warehousetealog;
 
import cn.com.hfuu.service.CodeService;
import cn.com.hfuu.service.TeaService;
import cn.com.hfuu.service.TealogService;
import cn.com.hfuu.service.UserService;
import cn.com.hfuu.vo.Page;


@Controller
@RequestMapping("tealog")
public class TealogControl {

	
	@Autowired
	CodeService codeService;
	
	@Autowired
	UserService userService;
	
	@Autowired
	TealogService tealogService;
	
	@Autowired
	TeaService teaService;
	
	List<code> codelist;
	@RequestMapping("info")
	public  String info(Model m){
		codelist=codeService.selectbyType("07");
		m.addAttribute("codelist", codelist);
		return "warehouse/tealog/tealog";
	}
	
	@RequestMapping("list")
	@ResponseBody
	public Page list(Integer start, Integer limit, warehousetealog tealog){
		System.out.println(tealog.getWarehouseTeatype());

		List<warehousetealog> list=tealogService.selectPage(start,limit,tealog);
		List<warehousetealog> total=tealogService.selectpagetotal(tealog);
		for (warehousetealog tlog : list) {
			tlog.setUser(userService.selectuserbyid(tlog.getWarehouseLoguserid()));
			for (code c : codelist) {
				if(c.getCodeValue()==tlog.getWarehouseTeatype())
					tlog.setCode(c);
			}
			tlog.setWarehousetea(teaService.selectbyid(tlog.getWarehouseTeaid()));
		}
		Page page = new Page();
		page.setStart(start);
		page.setLimit(limit);
		page.setRoot(list);
		page.setTotal(total.size() );
		return page;
		
	}
	
	@RequestMapping("export")
	public ModelAndView export(ModelMap map) {
 
		warehousetealog tlog1=new warehousetealog();
		tlog1.setWarehouseTeatype(-1);
 		List<warehousetealog> list = tealogService.selectpagetotal(tlog1);
		List<Map<String, String>> mapList = new ArrayList<Map<String, String>>();
		for (warehousetealog tlog : list) {
			Map<String, String> map1 = new HashMap<String, String>();
			tlog.setUser(userService.selectuserbyid(tlog.getWarehouseLoguserid()));
			for (code c : codelist) {
				if(c.getCodeValue()==tlog.getWarehouseTeatype())
					tlog.setCode(c);
			}
			tlog.setWarehousetea(teaService.selectbyid(tlog.getWarehouseTeaid()));
			 
			
			map1.put("编号", tlog.getWarehouseTealogid());
			map1.put("操作",tlog.getCode().getCodeName());
			map1.put("茶名",tlog.getWarehousetea().getTeaName());
			map1.put("重量(克)",""+tlog.getWarehouseTeaquality());
			map1.put("时间",tlog.getWarehouseTealogtime());
			map1.put("负责人",tlog.getUser().getUserName());
 
			mapList.add(map1);

		}
		String[] title = { "编号", "操作","茶名", "重量(克)","时间","时间", };
		map.addAttribute("excelList", mapList);
		ViewExcel ve = new ViewExcel(title, "茶叶库存日志信息");
		return new ModelAndView(ve, map);
	}
	
	warehousetealog tlog1;
	@RequestMapping("outlogedit")
	public ModelAndView edit(String id){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("warehouse/tealog/editout");
		tlog1=tealogService.selectbyid(id);
		warehousetea tea=teaService.selectbyid(tlog1.getWarehouseTeaid());
		List<warehousetea> tealist=teaService.selectall();
		mv.addObject("tealog", tlog1);
		mv.addObject("tea", tealist);
		
		return mv;
	}
	
	/**
	 * 修改出库
	 * @param tealog
	 * @return
	 */
	@RequestMapping("editoutproc")
	@ResponseBody
	public String outproc(warehousetealog tealog){
		warehousetealog logold=tealogService.selectbyid(tealog.getWarehouseTealogid());
		warehousetea teaold=teaService.selectbyid(tlog1.getWarehouseTeaid());//修改之前的产品信息
		if(tealog.getWarehouseTeaid().equals(logold.getWarehouseTeaid())){
			//未改变出库茶叶，更新茶叶质量
			float total=teaold.getTeaQuality()+tlog1.getWarehouseTeaquality()-tealog.getWarehouseTeaquality();
			if(total<0){
				return "error";
			}else{
				teaold.setTeaQuality(total);
				teaService.update(teaold);//更新库存
				
				if(tealogService.update(tealog)>0){//更新记录
					return  "success";
				}
			}
			
		}else{//改变了出库茶叶
			warehousetea tea=teaService.selectbyid(tealog.getWarehouseTeaid());
			//修改之前的产品
			teaold.setTeaQuality(teaold.getTeaQuality()+tlog1.getWarehouseTeaquality());
			if(tea.getTeaQuality()-tealog.getWarehouseTeaquality()<0){
				return "error";
			}
			//修改现在的
			tea.setTeaQuality(tea.getTeaQuality()-tealog.getWarehouseTeaquality ());
			teaService.update(teaold);
			teaService.update(tea);
			
			//更新日志
			if(tealogService.update(tealog)>0){
				return "success";
			}
			
		}
		return null;
		
	}
	
	@RequestMapping("delete")
	@ResponseBody
	public String outdelete(String id){
		warehousetealog tealog=tealogService.selectbyid(id);
		warehousetea tea=teaService.selectbyid(tealog.getWarehouseTeaid());
		tea.setTeaQuality(tea.getTeaQuality()+tealog.getWarehouseTeaquality());
		teaService.update(tea);
		if(tealogService.delete(id)>0){
			return "success";
		}
		return null;
	}
}
